package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.service.IOptionService;
import com.woniuxy.educationplatform.service.IQuesService;
import com.woniuxy.educationplatform.service.ITestTypeService;
import com.woniuxy.educationplatform.service.impl.QuesServiceImpl;
import com.woniuxy.educationplatform.util.PageDto;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/ques")
@RestController
public class QuesController {

        @Resource
        private IQuesService quesServiceImpl;
        @Resource
        private ITestTypeService testTypeServiceImpl;
        @Resource
        private IOptionService optionServiceImpl;

        /**
         * 查询所有题干
         * @param
         * @return
         */

        @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
        public List<QuesBean> testOne(QuesBean quesBean){
                System.out.println(quesBean);
                ResultUtil resultUtil = new ResultUtil();
                List<QuesBean> quesBeans = quesServiceImpl.findAll(quesBean);
                System.out.println(quesBeans);
                return quesBeans;
        }
        /* @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
        public List<QuesBean> findAll(){

            return quesServiceImpl.findAll();
        }
*/

        /**
         * 新增
         * @param quesBean
         * @return
         */
        @PostMapping(value="/more",produces = {"application/json;charset=utf-8"})
        public ResultMsg addQues(QuesBean quesBean){
                ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
                //盐值 加密   (按照现实生活中：不同的人抄相同的菜，根据盐的分量来区别味道)
//        String pwd = new SimpleHash("MD5",adminBean.getPassword(),
//                adminBean.getLoginName(),1024).toString();
                quesServiceImpl.addQuesBean(quesBean);
                return resultMsg;
        }

        /**
         * 删除
         * @param ids
         * @return
         */
        @RequestMapping(value="/{ids}",produces = {"application/json;charset=utf-8"})
        public ResultMsg delete(@PathVariable("ids") Long[] ids){
                ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);

                for (Long id:ids) {
                        quesServiceImpl.deleteQuesBean(id);
                }
                return resultMsg;
        }

        /**
         * 修改题干
         * @param quesBean
         * @param id
         * @return
         */
        @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
        public ResultMsg updateQues(QuesBean quesBean, @PathVariable("id") Long id){
                ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
                quesServiceImpl.updateQuesBean(quesBean);
                return resultMsg;
        }


        //查询所有的试卷类型
        @RequestMapping(value="/testtype",produces = {"application/json;charset=utf-8"})
        public List<TestType> findAll(){
                return testTypeServiceImpl.findAll();
        }



        @PostMapping(value="/addques",produces = {"application/json;charset=utf-8"})
        public void addques(Long testID, String type, String mark, String textarea, String textarea2
                , Integer[] checkList, String[] option0fq){

                QuesBean quesBean = new QuesBean();
                        TestBean testBean = new TestBean();//题目id
                        testBean.setId(testID);
                        quesBean.setTestBean(testBean);

                        QuesType quesType = new QuesType();
                        quesType.setId(Integer.valueOf(type));
                        quesBean.setQuesType(quesType);//题型id
                quesBean.setPointValue(Integer.valueOf(mark));//分数
                quesBean.setQuesContent(textarea);//题干
                quesServiceImpl.addQuesBean(quesBean);//插入题干



                for (int i = 0; i< option0fq.length; i++){
                        OptionBean optionBean = new OptionBean();
                        optionBean.setOptContent(option0fq[i]);

                        if(Arrays.asList(checkList).contains(i)){
                                optionBean.setIsRight(1);
                        }else {
                                optionBean.setIsRight(0);
                        }

                        optionBean.setTestBean(testBean);
                        optionBean.setQuesBean(quesBean);
                        //接下来，插入选项表
                        optionServiceImpl.insert(optionBean);

                }
        }


        @RequestMapping(value="/selectbyid",produces = {"application/json;charset=utf-8"})
        public QuesBean getOneById(Long id){
                return quesServiceImpl.getOneById(id);
        }

        /**
         * 修改
         * @param quesBean
         */
        @PostMapping(value="/update",produces = {"application/json;charset=utf-8"})
        public void updateQuesBean(QuesBean quesBean){

                System.out.println(quesBean);
                quesServiceImpl.updateQuesBean(quesBean);

        }

        /**
         * 删除题目
         * @param id
         * @return
         */
        @DeleteMapping("/del/{id}")
        public ResultUtil delById(@PathVariable Integer id){
                ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
                if((quesServiceImpl.deleteQuesBean(Long.valueOf(id)))>0){
                        resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
                }
                return  resultUtil;
        }







}
